<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生修改</title>
</head>
<style>
    #content {
        margin: 50px auto;
        width: 650px;
        border: solid 1px #6699FF;
        padding: 30px;
    }

    #left {
        float: left;
        width: 350px;
        height: 400px;
    }

    #right {
        float: left;
        width: 300px;
        height: 400px;
    }

    #bottom {
        clear: both;
        width: 600px;
    }

    #pic {
        width: 120px;
        height: 160px;
        background: #CCC;
        padding: 10px;
    }

    #pic img {
        width: 120px;
        height: 160px;
    }

    .error {
        color: #F00;
    }
</style>
<script type="text/javascript">
    window.onload = function() {
        var fileTag = document.getElementById("file");
        fileTag.onchange = function() {
            var file = fileTag.files[0];
            if (!/image\/\w+/.test(file.type)) {
                alert("你好，选择的不是图片！");
                return false;
            }
            var fileReader = new FileReader();
            fileReader.onloadend = function() {
                if (fileReader.readyState == fileReader.DONE) {
                    document.getElementById("img").setAttribute("src", fileReader.result);
                }
            };
            fileReader.readAsDataURL(file);
        };
    };
</script>

<body>
    <?php
    include "isLogin.php";
    include "conn.php";
    $stu_get=$_GET["stu_no"];
    $sql = "select * from major";
    $result = mysqli_query($conn, $sql) or die("数据查询失败");
    $sql = "select * from student where stu_no='$stu_get'";
    $result_stu = mysqli_query($conn, $sql) or die("数据查询失败");
    $row_stu = mysqli_fetch_assoc($result_stu);
    function filterInput($data)
    {
        $data = trim($data); //不必要的字符 (如：空格，tab，换行)。
        $data = stripslashes($data); //去除反斜杠 (\)
        $data = htmlspecialchars($data); //把一些预定义的字符转换为 HTML 实体
        return $data;
    }
    //定义错误提示变量
    $noErr = $nameErr = $majorErr = $photoErr = $telephoneErr = $emailErr = $birthdateErr = "";
    if ($_SERVER["REQUEST_METHOD"] == "POST") { //如果是提交表单
        //获得各表单项的值
        $stu_no = $_POST["stu_no"]; //学号
        $stu_name = $_POST["stu_name"]; //姓名
        $major = $_POST["major"]; //专业
        $gender = $_POST["gender"]; //性别
        $birthdate = $_POST["birthdate"]; //出生日期
        $telephone = $_POST["telephone"]; //手机
        $email = $_POST["email"]; //邮箱
        @$resume = $_POST["resume"]; //个人简介
        //过滤字符
        $stu_no = filterInput($stu_no);
        $stu_name = filterInput($stu_name);
        //设置必填项	
        if (empty($stu_no)) {
            $noErr = "学号为空";
        };
        if (empty($stu_name)) {
            $nameErr = "姓名为空";
        };
        if (empty($major)) {
            $majorErr = "专业为空";
        };

        if (empty($birthdate)) {
            $birthdate = "1970-01-01";
        } else {
            if (!(preg_match("/^\d{4}-\d{1,2}-\d{1,2}/", $birthdate))) {
                $birthdateErr = "日期不规范";
            }
        };
        if (!(empty($telephone))) {
            if (!(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/", $telephone))) {
                $telephoneErr = "电话号码不规范";
            }
        };
        if (!(empty($email))) {
            if (!(preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $email))) {
                $emailErr = "邮箱不规范";
            }
        };
        if (($noErr == '') && ($nameErr == '') && ($majorErr == '') && ($birthdateErr == '') && ($telephoneErr == '') && ($emailErr == '')) {
            //判断学号是否重复   
            $sql = "select * from student where stu_no=$stu_no and stu_no <> '$stu_get'";
            $result1 = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result1) > 0) {
                $noErr = "学号重复";
            } else {
                //移动文件到指定位置
                $photo = ""; //照片路径及文件名设置为空串
                if (!(empty($_FILES["file"]["name"]))) { //如果上传的文件名不为空			 				
                    if ($_FILES["file"]["error"] > 0) { //如果上传没有错误
                        $photoErr = "照片上传失败，错误号:" . $_FILES["file"]["error"]; //上传出错的话，显示错误号
                    } else {
                        move_uploaded_file($_FILES["file"]["tmp_name"], "./photos/" . $_FILES["file"]["name"]);
                        $photo = "photos/" . $_FILES["file"]["name"];
                    } //end of if($_FILES["file"]["error"] > 0)
                } else{
                    $photoErr ="照片为空";
                } //end of  if(!(empty($_FILES["file"]["name"])))
                //插入数据
               
                // $sql = "insert into student(stu_no,stu_name,major_id,gender,birthdate,telephone,email,photo,resume) value('$stu_no','$stu_name','$major','$gender','$birthdate','$telephone','$email','$photo','$resume')";
                $sql="update student set stu_no='$stu_no',stu_name='$stu_name', major_id='$major',gender='$gender',birthdate='$birthdate',telephone='$telephone',
                    email='$email', photo='$photo', resume='$resume'  where stu_no='$stu_get'";
                $result2 = mysqli_query($conn, $sql) or die("插入失败" . $sql);
                echo "<script language='javascript' type='text/javascript'>";
                echo "alert('学生添加成功');";
                echo "</script>";
                //跳转到学生详情页面
                header("location:stuBrowse.php");
            } //end of  if(mysqli_num_rows($result1)>0)
        }
    } //end of  if(($noErr=='') &&($nameErr=='')
    ?>
    <form name="form" method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
        <div id="content">
            <div id="left">
                <p>
                    <label for="stu_on">学号</label>
                    <span><input type="text" name="stu_no" id="stu_no" value="<?php echo $row_stu["stu_no"] ?>" /> </span>
                    <span class='error'>*<?php echo $noErr; ?></span>
                </p>
                <p><label >姓名</label><span><input type="text" name="stu_name" value="<?php echo $row_stu["stu_name"] ?>" /></span>
                    <span class='error'>*<?php echo $nameErr; ?></span>
                </p>
                <p>
                    <label>专业</label>
                    <span>
                        <select name="major" autocomplete="off">
                            <option value="">===请选择===</option>
                            <?php while ($row = mysqli_fetch_assoc($result)) { ?>
                                <option value="<?php echo $row['major_id'] ?> " 
                                <?php if (  $row['major_id'] == $row_stu['major_id'])
                                echo "selected = 'selected' "?>  
                                >     
                                <?php echo $row['major_name'] ?></option> 
                            <?php } ?>
                            <!-- <optionvalue="01">电子商务</option>
                                <optionvalue="02">信息管理</option>
                                    <optionvalue="03">计算机应用</option>
                                        <optionvalue="04">软件工程</option> -->
                        </select>
                    </span>
                    <span class="error">*<?php echo $majorErr; ?></span>
                </p>
                <p><label>性别</label>
                    <span>男<input name="gender" type="radio" value="男" <?php if (
                                                                            $row_stu["gender"] == '男'
                                                                        ) echo "checked" ?> />
                        女<input name="gender" type="radio" value="女" <?php if (
                                                                            $row_stu["gender"] == '女'
                                                                        ) echo "checked" ?> />
                    </span>
                </p>
                <p><label>生日</label><span><input type="date" name="birthdate" value="<?php echo $row_stu["birthdate"] ?>" /></span>
                    <span class="error">*<?php echo $birthdateErr; ?></span>
                </P>
                <p><label>电话</label><span><input type="text" name="telephone" value="<?php echo $row_stu["telephone"] ?>" /></span>
                    <span class="error">*<?php echo $telephoneErr; ?></span>
                </p>
                <p><label>邮箱</label><span><input type="text" name="email" value="<?php echo $row_stu["email"] ?>" /></span>
                    <span class="error">*<?php echo $emailErr; ?></span>
                </p>
            </div>
            <div id="right">
                <p>
                    <label>照片</label>
                    <span>
                       
                        <input type="file" name="file" id="file" />
                    </span>
                    <span class="error">*<?php echo $photoErr; ?></span>
                </p>
                <div id="pic">
                    <img id="img" src="<?php if (empty($row_stu["photo"])) {
                        echo "./images/head.png";
                    } else echo $row_stu["photo"] ?>" />
                </div>
            </div>
            <div id="bottom">
                <p>个人简介</p>
                <!-- <textarea name="resume" rows="10" cols="80"> -->
                <!-- </textarea> -->
                <!-- 富文本编辑器 -->
                <script id="container" name="resume" type="text/plain">
                    <?php echo $row_stu["resume"]  ?>
                    </script>

                <p>
                    <input name="submit" type="submit" value="提交" />
                    <input name="reset" type="reset" value="重置" />
                </p>
            </div>
        </div>
    </form>
    <!-- 配置文件 -->

    <script type="text/javascript" src=".\ueditor\ueditor.config.js"></script>

    <!-- 编辑器源码文件 -->

    <script type="text/javascript" src=".\ueditor\ueditor.all.js"></script>

    <!-- 实例化编辑器 -->

    <script type="text/javascript">
        var editor = UE.getEditor('container');
    </script>
</body>

</html>